package com.xingfudazi.app.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xingfudazi.app.domain.AppMemberInfo;
import com.xingfudazi.app.domain.AppMomentComment;
import com.xingfudazi.app.mapper.AppMomentCommentMapper;
import com.xingfudazi.app.service.IAppMemberInfoService;
import com.xingfudazi.app.service.IAppMomentCommentService;
import com.xingfudazi.app.service.IAppMomentService;
import com.xingfudazi.common.enums.DBDataStatus;
import com.xingfudazi.common.exception.ServiceException;
import com.xingfudazi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 动态-评论Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-21
 */
@Service
public class AppMomentCommentServiceImpl implements IAppMomentCommentService {
    @Autowired
    private AppMomentCommentMapper appMomentCommentMapper;

    @Autowired
    private IAppMemberInfoService appMemberInfoService;

    @Autowired
    private IAppMomentService appMomentService;

    /**
     * 查询动态-评论
     *
     * @param id 动态-评论主键
     * @return 动态-评论
     */
    @Override
    public AppMomentComment selectAppMomentCommentById(Long id) {
        return appMomentCommentMapper.selectById(id);
    }

    /**
     * 查询动态-评论列表
     *
     * @param appMomentComment 动态-评论
     * @return 动态-评论
     */
    @Override
    public List<AppMomentComment> selectAppMomentCommentList(AppMomentComment appMomentComment) {
        LambdaQueryWrapper<AppMomentComment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AppMomentComment::getDelFlag, DBDataStatus.OK.getCode());
        Long mid = appMomentComment.getMid();
        wrapper.eq(AppMomentComment::getMid, mid);

        wrapper.orderByAsc(AppMomentComment::getCreateTime);
        return appMomentCommentMapper.selectList(wrapper);
    }

    /**
     * 新增动态-评论
     *
     * @param appMomentComment 动态-评论
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertAppMomentComment(AppMomentComment appMomentComment) {
        Long nid = SecurityUtils.getUserId();
        AppMemberInfo appMemberInfo = this.appMemberInfoService.selectAppMemberInfoById(nid);
        appMomentComment.setNid(nid);
        appMomentComment.setNickName(appMemberInfo.getNickName());
        appMomentComment.setAvatar(appMemberInfo.getAvatar());

        appMomentCommentMapper.insert(appMomentComment);

        Long mid = appMomentComment.getMid();
        if (mid != null) {
            this.appMomentService.comment(mid, 1);
        }

        return 1;
    }

    @Override
    public int updateAppMomentComment(AppMomentComment appMomentComment) {
        return 0;
    }


    /**
     * 删除动态-评论信息
     *
     * @param id 动态-评论主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteAppMomentCommentById(Long id) {
        AppMomentComment oData = appMomentCommentMapper.selectById(id);
        if (oData == null || !oData.getNid().equals(SecurityUtils.getUserId())) {
            throw new ServiceException("不能删除别人的数据啊");
        }
        AppMomentComment appMomentComment = new AppMomentComment();
        appMomentComment.setDelFlag(DBDataStatus.DELETED.getCode());
        appMomentComment.setId(id);
        appMomentCommentMapper.updateById(appMomentComment);

        Long mid = appMomentComment.getMid();
        if (mid != null) {
            this.appMomentService.comment(mid, -1);
        }
        return 1;
    }


}
